﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CP2011_Assignment
{
    public partial class EditProductionUI2 : Form
    {
        private int invId; 
       public EditProductionUI2(int invId)
        {
            InitializeComponent();
            this.invId = invId;
        }
      
        private void executeEdit(IDataController dataCtrl, String editQuery)
        {
            if (MessageBox.Show("Are you sure you want to edit item with Inventory ID =" + invId + "?", "Confirm update", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                dataCtrl.editRecord(editQuery, invId);
                
                this.Hide();
                this.Owner.Show();
                this.Dispose();
            }
        }

        private void btnEdit_Click_1(object sender, EventArgs e)
        {
            TextBox[] textBoxArr = new TextBox[4];
            textBoxArr[0] = txtBox1;
            textBoxArr[1] = txtBox2;
            textBoxArr[2] = txtBox3;
            textBoxArr[3] = txtBox4;

            for (int i = 0; i < 4; i++)
            {
                //check whether the input is empty or not
                if (textBoxArr[i].Text.Length != 0)
                {
                    switch (i)
                    {
                        case 0:
                            String editNameQuery = "UPDATE Inventory SET Inv_Name = '" + txtBox1.Text + "'WHERE Inv_ID = " + invId;
                            executeEdit(new InventorySystem(), editNameQuery);
                            break;
                        case 1:
                            String editDescQuery = "UPDATE Inventory SET Inv_Desc = '" + txtBox2.Text + "'WHERE Inv_ID = " + invId;
                            executeEdit(new InventorySystem(), editDescQuery);
                            break;
                        case 2:
                            String editQtyQuery = "UPDATE Inventory SET Inv_QtyPerMonth = " + Convert.ToInt32(txtBox3.Text) + "WHERE Inv_ID = " + invId;
                            executeEdit(new InventorySystem(), editQtyQuery);
                            break;
                        case 3:
                            String editSupplier = "UPDATE Inventory SET Inv_Supplier = " + Convert.ToInt32(txtBox4.Text) + "WHERE Inv_ID = " + invId;
                            executeEdit(new InventorySystem(), editSupplier);
                            break;
                    }
                }
            }

        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Hide();
            this.Owner.Show();
            this.Dispose();
        }

        
    }
}
